package com.hsmpay.common.util.memcached;

import com.hsmpay.common.util.system.Constant;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * 单列的Memcached
 * windows 服务器使用
 *
 * @author 颜铃璋
 */
public class MemcachedUtil {
    private static MemcachedClient memcachedClient;
    public static String MEMCACHE_URL = Constant.getAttribute("MEMCACHE_URL");
    private static Logger log = Logger.getLogger(MemcachedUtil.class);

    static {

		/*MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));
		memcachedClient = builder.build();*/

        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(MEMCACHE_URL), new int[]{1, 3});
        //builder.addAuthInfo(AddrUtil.getOneAddress("localhost:11211"), AuthInfo.typical("cacheuser", "123456"));
        // Must use binary protocol
        //builder.setCommandFactory(new BinaryCommandFactory());

        try {
            memcachedClient = builder.build();
        } catch (IOException e) {
            //e.printStackTrace();
            memcachedClient = null;
            log.error("###Memcached 连接 :" + MemcachedUtil.MEMCACHE_URL + "失败！###");
        }
    }

    public static MemcachedClient memcachedFactory() {
        return memcachedClient;
    }

}
